package com.leetcode.partition7;

import java.io.*;

/**
 * @author `RKC`
 * @date 2021/12/28 11:51
 */
public class LC676实现一个魔法字典 {

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = reader.readLine().split("\\s+");
        String[] searchWords = reader.readLine().split("\\s+");
        MagicDictionary dictionary = new MagicDictionary();
        dictionary.buildDict(s);
        for (String searchWord : searchWords) {
            writer.write(dictionary.search(searchWord) + " ");
        }
        writer.flush();
    }

    private static class MagicDictionary {

        private final int N = 10010;
        private final int[][] trieNode = new int[N][26];
        private final boolean[] isWord = new boolean[N];
        private int index = 1;

        public MagicDictionary() { }

        public void buildDict(String[] dictionary) {
            for (String s : dictionary) {
                int curr = 0;
                for (int i = 0; i < s.length(); i++) {
                    int child = s.charAt(i) - 'a';
                    if (trieNode[curr][child] == 0) trieNode[curr][child] = index++;
                    curr = trieNode[curr][child];
                }
                isWord[curr] = true;
            }
        }

        public boolean search(String searchWord) {
            return search(searchWord.toCharArray(), 0, 0, 0);
        }

        private boolean search(char[] word, int start, int cnt, int root) {
            if (cnt > 1) return isWord[start];
            int curr = root;
            for (int i = start; i < word.length; i++) {
                int child = word[i] - 'a';
                if (cnt < 1) {
                    char ch = word[i];
                    for (int j = 0; j < 26; j++) {
                        if (trieNode[curr][j] == 0 || 'a' + j == ch) continue;
                        word[i] = (char) ('a' + j);
                        if (search(word, i, cnt + 1, curr)) return true;
                    }
                    word[i] = ch;
                }
                if (trieNode[curr][child] == 0) return false;
                curr = trieNode[curr][child];
            }
            return isWord[curr] && cnt == 1;
        }
    }
}
